/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.banco;

import br.edu.util.GenericDAO;
import br.edu.util.HibernateUtil;
import java.util.List;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author 'Pedro
 */
public class BancoDAO extends GenericDAO<Banco> {

    public BancoDAO() {
        super(Banco.class);
    }

    public boolean salvar(Banco banco) {
        if (banco.getId() == 0) {
            adicionar(banco);
        } else {
            atualizar(banco);
        }

        return true;
    }

    public List<Banco> pesquisarDescricaoLike(String text) {
        List<Banco> bancos = null;
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        bancos = (List<Banco>) getSessao().createCriteria(Banco.class).add(Restrictions.ilike("descricao", text)).list();
        getSessao().close();
        return bancos;
    }

    public Banco pesquisarCodigo(Integer valueOf) {
        Banco banco;
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        banco = (Banco) getSessao().createCriteria(Banco.class).add(Restrictions.eq("id", valueOf)).uniqueResult();
        getSessao().close();
        return banco;
    }
}
